home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Net / DNS / Header.php next >
PHP Script  |  2004-03-24  |  9KB  |  290 lines

  1. <?php
  2. /*
  3.  *  License Information:
  4.  *
  5.  *    Net_DNS:  A resolver library for PHP
  6.  *    Copyright (C) 2002 Eric Kilfoil eric@ypass.net
  7.  *
  8.  *    This library is free software; you can redistribute it and/or
  9.  *    modify it under the terms of the GNU Lesser General Public
  10.  *    License as published by the Free Software Foundation; either
  11.  *    version 2.1 of the License, or (at your option) any later version.
  12.  *
  13.  *    This library is distributed in the hope that it will be useful,
  14.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  *    Lesser General Public License for more details.
  17.  *
  18.  *    You should have received a copy of the GNU Lesser General Public
  19.  *    License along with this library; if not, write to the Free Software
  20.  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  */
  22.  
  23. /*  Net_DNS_Header object definition {{{ */
  24. /**
  25.  * Object representation of the HEADER section of a DNS packet
  26.  *
  27.  * The Net_DNS::Header class contains the values of a DNS  packet.  It parses
  28.  * the header of a DNS packet or can  generate the binary data
  29.  * representation of the packet.  The format of the header is described in
  30.  * RFC1035.
  31.  *
  32.  * @package Net_DNS
  33.  */
  34. class Net_DNS_Header
  35. {
  36.     /* class variable definitions {{{ */
  37.     /**
  38.      * The packet's request id
  39.      *
  40.      * The request id of the packet represented as  a 16 bit integer.
  41.      */
  42.     var $id;
  43.     /**
  44.      * The QR bit in a DNS packet header
  45.      *
  46.      * The QR bit as described in RFC1035.  QR is set to 0 for queries, and
  47.      * 1 for repsones.
  48.      */
  49.     var $qr;
  50.     /**
  51.      * The OPCODE name of this packet.
  52.      *
  53.      * The string value (name) of the opcode for the DNS packet.
  54.      */
  55.     var $opcode;
  56.     /**
  57.      * The AA (authoritative answer) bit in a DNS packet header
  58.      *
  59.      * The AA bit as described in RFC1035.  AA is set to  1 if the answer
  60.      * is authoritative.  It has no meaning if QR is set to 0.
  61.      */
  62.     var $aa;
  63.     /**
  64.      * The TC (truncated) bit in a DNS packet header
  65.      *
  66.      * This flag is set to 1 if the response was truncated.  This flag has
  67.      * no meaning in a query packet.
  68.      */
  69.     var $tc;
  70.     /**
  71.      * The RD (recursion desired) bit in a DNS packet header
  72.      *
  73.      * This bit should be set to 1 in a query if recursion  is desired by
  74.      * the DNS server.
  75.      */
  76.     var $rd;
  77.     /**
  78.      * The RA (recursion available) bit in a DNS packet header
  79.      *
  80.      * This bit is set to 1 by the DNS server if the server is willing to
  81.      * perform recursion.
  82.      */
  83.     var $ra;
  84.     /**
  85.      * The RCODE name for this packet.
  86.      *
  87.      * The string value (name) of the rcode for the DNS packet.
  88.      */
  89.     var $rcode;
  90.     /**
  91.      * Number of questions contained within the packet
  92.      *
  93.      * 16bit integer representing the number of questions in the question
  94.      * section of the DNS packet.
  95.      *
  96.      * @var integer $qdcount
  97.      * @see     Net_DNS_Question class
  98.      */
  99.     var $qdcount;
  100.     /**
  101.      * Number of answer RRs contained within the packet
  102.      *
  103.      * 16bit integer representing the number of answer resource records
  104.      * contained in the answer section of the DNS packet.
  105.      *
  106.      * @var integer $ancount
  107.      * @see     Net_DNS_RR class
  108.      */
  109.     var $ancount;
  110.     /**
  111.      * Number of authority RRs within the packet
  112.      *
  113.      * 16bit integer representing the number of authority (NS) resource
  114.      * records  contained in the authority section of the DNS packet.
  115.      *
  116.      * @var integer $nscount
  117.      * @see     Net_DNS_RR class
  118.      */
  119.     var $nscount;
  120.     /**
  121.      * Number of additional RRs within the packet
  122.      *
  123.      * 16bit integer representing the number of additional resource records
  124.      * contained in the additional section of the DNS packet.
  125.      *
  126.      * @var integer $arcount
  127.      * @see     Net_DNS_RR class
  128.      */
  129.     var $arcount;
  130.  
  131.     /* }}} */
  132.     /* class constructor - Net_DNS_Header($data = "") {{{ */
  133.     /**
  134.      * Initializes the default values for the Header object.
  135.      * 
  136.      * Builds a header object from either default values, or from a DNS
  137.      * packet passed into the constructor as $data
  138.      *
  139.      * @param string $data  A DNS packet of which the header will be parsed.
  140.      * @return  object  Net_DNS_Header
  141.      * @access public
  142.      */
  143.     function Net_DNS_Header($data = "")
  144.     {
  145.         if ($data != "") {
  146.             /*
  147.              * The header MUST be at least 12 bytes.
  148.              * Passing the full datagram to this constructor
  149.              * will examine only the header section of the DNS packet
  150.              */
  151.             if (strlen($data) < 12)
  152.                 return(0);
  153.  
  154.             $a = unpack("nid/C2flags/n4counts", $data);
  155.             $this->id      = $a["id"];
  156.             $this->qr      = ($a["flags1"] >> 7) & 0x1;
  157.             $this->opcode  = ($a["flags1"] >> 3) & 0xf;
  158.             $this->aa      = ($a["flags1"] >> 2) & 0x1;
  159.             $this->tc      = ($a["flags1"] >> 1) & 0x1;
  160.             $this->rd      = $a["flags1"] & 0x1;
  161.             $this->ra      = ($a["flags2"] >> 7) & 0x1;
  162.             $this->rcode   = $a["flags2"] & 0xf;
  163.             $this->qdcount = $a["counts1"];
  164.             $this->ancount = $a["counts2"];
  165.             $this->nscount = $a["counts3"];
  166.             $this->arcount = $a["counts4"];
  167.         }
  168.         else {
  169.             $this->id      = Net_DNS_Resolver::nextid();
  170.             $this->qr      = 0;
  171.             $this->opcode  = 0;
  172.             $this->aa      = 0;
  173.             $this->tc      = 0;
  174.             $this->rd      = 1;
  175.             $this->ra      = 0;
  176.             $this->rcode   = 0;
  177.             $this->qdcount = 1;
  178.             $this->ancount = 0;
  179.             $this->nscount = 0;
  180.             $this->arcount = 0;
  181.         }
  182.  
  183.         if (Net_DNS::opcodesbyval($this->opcode)) {
  184.             $this->opcode = Net_DNS::opcodesbyval($this->opcode);
  185.         }
  186.         if (Net_DNS::rcodesbyval($this->rcode)) {
  187.             $this->rcode = Net_DNS::rcodesbyval($this->rcode);
  188.         }
  189.     }
  190.  
  191.     /* }}} */
  192.     /* Net_DNS_Header::display() {{{ */
  193.     /**
  194.      * Displays the properties of the header.
  195.      *
  196.      * Displays the properties of the header.
  197.      *
  198.      * @access public
  199.      */
  200.     function display()
  201.     {
  202.         echo $this->string();
  203.     }
  204.  
  205.     /* }}} */
  206.     /* Net_DNS_Header::string() {{{ */
  207.     /**
  208.      * Returns a formatted string containing the properties of the header.
  209.      *
  210.      * @return string   a formatted string containing the properties of the header.
  211.      * @access public
  212.      */
  213.     function string()
  214.     {
  215.         $retval = ";; id = " . $this->id . "\n";
  216.         if ($this->opcode == "UPDATE") {
  217.             $retval .= ";; qr = " . $this->qr . "    " .
  218.                 "opcode = " . $this->opcode . "    "   .
  219.                 "rcode = " . $this->rcode . "\n";
  220.             $retval .= ";; zocount = " . $this->qdcount . "  " .  
  221.                 "prcount = " . $this->ancount . "  "           .
  222.                 "upcount = " . $this->nscount . "  "           .
  223.                 "adcount = " . $this->arcount . "\n";
  224.         } else {
  225.             $retval .= ";; qr = " . $this->qr . "    " .
  226.                 "opcode = " . $this->opcode . "    "   .
  227.                 "aa = " . $this->aa . "    "           .
  228.                 "tc = " . $this->tc . "    "           .
  229.                 "rd = " . $this->rd . "\n";
  230.  
  231.             $retval .= ";; ra = " . $this->ra . "    " .
  232.                 "rcode  = " . $this->rcode . "\n";
  233.  
  234.             $retval .= ";; qdcount = " . $this->qdcount . "  " .
  235.                 "ancount = " . $this->ancount . "  "    .
  236.                 "nscount = " . $this->nscount . "  "    .
  237.                 "arcount = " . $this->arcount . "\n";
  238.         }
  239.         return($retval);
  240.     }
  241.  
  242.     /* }}} */
  243.     /* Net_DNS_Header::data() {{{ */
  244.     /**
  245.      * Returns the binary data containing the properties of the header
  246.      *
  247.      * Packs the properties of the Header object into a binary string
  248.      * suitable for using as the Header section of a DNS packet.
  249.      *
  250.      * @return string   binary representation of the header object
  251.      * @access public
  252.      */
  253.     function data()
  254.     {
  255.         $opcode = Net_DNS::opcodesbyname($this->opcode);
  256.         $rcode  = Net_DNS::rcodesbyname($this->rcode);
  257.  
  258.         $byte2 = ($this->qr << 7)
  259.             | ($opcode << 3)
  260.             | ($this->aa << 2)
  261.             | ($this->tc << 1)
  262.             | ($this->rd);
  263.  
  264.         $byte3 = ($this->ra << 7) | $rcode;
  265.  
  266.         return pack("nC2n4", $this->id,
  267.                 $byte2,
  268.                 $byte3,
  269.                 $this->qdcount,
  270.                 $this->ancount,
  271.                 $this->nscount,
  272.                 $this->arcount);
  273.     }
  274.  
  275.     /* }}} */
  276. }
  277. /* }}} */
  278. /* VIM settings {{{
  279.  * Local variables:
  280.  * tab-width: 4
  281.  * c-basic-offset: 4
  282.  * soft-stop-width: 4
  283.  * c indent on
  284.  * expandtab on
  285.  * End:
  286.  * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  287.  * vim<600: sw=4 ts=4
  288.  * }}} */
  289. ?>
  290.